package comp
{
	use namespace comp_layout;

	import comp.style.css.CssAchieve;
	
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Rectangle;

	/**
	 * <h1>tab导航按钮</h1>
	 *	支持 布局方式 0横排，1竖排,margin
	 * clickFunction点击tab按钮调用的function(e:MouseEvent)
	 * </br>
	 * tabar中得按钮主题名 comp::TabBar.Button
	 * </br>
	 * 示例:webgame.missionFollow.followView 139行,webgame.mission.MisView 123行
	 * @author:pu
	 */ 
	public class TabBar extends ListBase
	{
		include "style/SkinStyle" //皮肤属性

		//**************样式*****************

		//public 
		public function TabBar()
		{
			super();
			super.onItemBind=itemBind;
			super.onChange=_onTbChange;
			this._themeName="comp::TabBar";
			_buttons=new Array();

		}
		//**************** override *****************
		/**
		 * horizontal[水平排列，默认],vertical[垂直],accordion[手风琴方式布局，替换accordion控件]		
		 */
		override public function set layout(value:String):void
		{
			_layout = value;
		}
		
		override public function set enabled(value:Boolean):void
		{
			super.enabled=value;
			if(this.initialized)
				setFilter();
		}
		
		protected override function merge():void
		{
			var lay:String=getStyleDefault("layout","horizontal");//默认水平排列
			var ml:Number=getStyle("marginLeft");
			var mr:Number=getStyle("marginRight");
			var mt:Number=getStyle("marginTop");
			var mb:Number=getStyle("marginBottom");
			var lx:Number=0,ly:Number=0;//上个对像x和上个对像y
			var panelx:Number=0,panely:Number=0;//用来确定panel的位置
			var i:uint=0;
			var child:DisplayObject;
			
			if(lay.toLocaleLowerCase()=="vertical") //垂直 类按垂直顺序从上向下排列布局元素，在元素和围绕元素顺序的可选填充之间带有可选间隙。
			{	
				for(i=0;i<this.numChildren;i++)
				{
					child=this.getChildAt(i);
					if(child is Button && child.visible==true)// ||  !(child is  ScrollBar)
					{
						child.x=ml;
						child.y=ly+mt;	
						panelx=child.width+mr;
						panely=mt;
						ly=child.y+child.height+mb;
					}
				}
				
				if(this._list && _list.length>=this.data.length)
				{
					for(i=0;i<_list.length;i++)
					{
						_list[i].x=panelx;
						_list[i].y=panely;
					}
				}
			}else if(lay.toLocaleLowerCase()=="accordion") //替换accordion控件
			{
				ly=showPanel();
			}
			else //水平
			{
				for(i=0;i<this.numChildren;i++)
				{
					child=this.getChildAt(i);
					if(child is Button && child.visible==true)// || !(child is ScrollBar)
					{
						child.x=lx+ml;
						child.y=mt;	
						panelx=ml;
						panely=mt+child.height;
						lx=child.x+child.width+mr;
					}
				}//end for
				if(this._list && _list.length>=this.data.length)
				{
					for(i=0;i<_list.length;i++)
					{
						_list[i].x=panelx;
						_list[i].y=panely;
					}
				}
			}
			
			if(this._autoH)
				this._myHeight=ly;
			
			this._maxHeight=ly;
			
			CssAchieve.mergeTemplate(this);
			
			setFilter();

			this.wholeRect=new Rectangle(0,0,this.width,this.height);

			if(this.parent && this.parent.parent is Panel)
				Panel(parent.parent).styleChanged("merge","");
		}
		
		private function setFilter():void
		{
			if(getStyle("enabled")==false)
			{
				if(!_colorFilter)
				{
					var matrix:Array=[0.3086, 0.6094, 0.0820, 0, 0,  
						0.3086, 0.6094, 0.0820, 0, 0,  
						0.3086, 0.6094, 0.0820, 0, 0,  
						0,      0,      0,      1, 0];  
					_colorFilter=new ColorMatrixFilter(matrix);
				}
				this.filters=[_colorFilter];
			}else
				this.filters=[];
		}
		
		protected override function createChildren():void
		{
			super.createChildren();
			
			this.addPanel();
		}
		
		override public function set  data(value:Array):void
		{
			_buttons.length=0;
			super.data=value;
		}
		
		/**
		 * 此类中不能修改此方法
		 * 结构必须为:
		 * function itemBind(i:uint):DisplayObject
		 */
		public override function set onItemBind(value:Function):void
		{
			//客户端口不能修改些方法
		}
		
		/**
		 * 此类中不能修改此方法
		 * 格式
		 * function onSelectedChange():void{}
		 */
		public override function set onChange(value:Function):void
		{
			//客户端口不能修改些方法
		}
		
		public function get preItemBind():Function
		{
			return _preItemBind;
		}
		
		/**
		 * 每个选择项绑定之前调用 样式Button style
		 * 格式function(i:int,data:*,btn:Button):object样式 返回样式对象或者null
		 */
		public function set preItemBind(value:Function):void
		{
			_preItemBind = value;
		}

		/**
		 * 点击选择项的时候调用的事件
		 * @e 选择的项
		 * function(e:MouseEvent):void;
		 */
		public function get clickFunction():Function
		{
			return _clickFunction;
		}

		public function set clickFunction(value:Function):void
		{
			_clickFunction = value;
		}

		
		public override function dispose():void
		{
			super.dispose();
			_clickFunction=null;
		}
	
		//****************** 内部使用 *************************
		
		protected function addPanel():void
		{
			if(this._list)
			{
				var l:DisplayObject;
				for(var i:int=0;i<_list.length;i++)
				{
					l=_list[i];
					if(i==this.selectedIndex)
						l.visible=true;
					else
						l.visible=false;
					this.addChild(l);
				}
				
			}
		}
		
		protected function createPanel():void
		{
			_list=new Array();
			if(this.data)
			{
				for(var i:int=0;i<data.length;i++)
				{
					var p:Panel=new Panel();
					p.useTheme=this.useTheme;
					p.themeName=this.themeName+".Panel";
					p.name=this.name+"_p_"+i;
					if(p.width==0)
						p.width=this.width;
					if(p.height==0)
						p.height=this.height;
					p.tag=data[i];
					_list.push(p);
				}
			}
		}
		
		/**
		 * 显示列表
		 */
		protected function showPanel():int
		{
			if(this._list && _list.length>=this.data.length)
			{
				if(this.lastSelected>=0)
					this._list[lastSelected].visible=false;
				
				if(this.selectedIndex>=0)
					this._list[selectedIndex].visible=true;
			}
			
			var lay:String=getStyleDefault("layout","horizontal");//默认水平排列
			if(lay.toLocaleLowerCase()=="accordion") //替换accordion控件
			{
				var i:int,child:DisplayObject,ly:Number=0,index:int=-1,panel:DisplayObject;
				var ml:Number=getStyle("marginLeft");
				var mr:Number=getStyle("marginRight");
				var mt:Number=getStyle("marginTop");
				var mb:Number=getStyle("marginBottom");
				
				for(i=0;i<this.numChildren;i++)
				{
					child=this.getChildAt(i);
					if(child is Button && child.visible==true)// ||  !(child is  ScrollBar)
					{
						child.x=ml;
						child.y=ly+mt;	
						index=this.getIndexByName(child.name);
						ly=child.y+child.height+mb;
						if(this._list && this._list.length>index)
						{
							panel=_list[index];
							if(panel.visible==true)
							{
								panel.x=child.x
								panel.y=ly+mt;
								ly=panel.y+panel.height+mb;
							}
						}
						
					}
				}
			}

			return ly;
		}
		
		private  function itemBind(i:uint,dataItem:Object):DisplayObject
		{
			var b:Button=new Button;		
			b.themeName=this.themeName+".Button";
			b.useTheme=this.useTheme;
			
			var sty:Object;
			if(this._preItemBind!=null)
				sty=_preItemBind(i,dataItem,b)
			
			if(sty!=null)
				b.style=sty;
			
			if(this.selectedIndex==i)
				b.selected=true;
		
			if(data[i] is String)
				b.text=data[i];
			else 
				b.text=data[i][this.showField].toString();
			
			if(getStyle("buttonClassName"))
				b.className=this.getStyle("buttonClassName");
			
			b.clickFunction=itemClick;
			
			_buttons.push(b);//添加进集合
			return b;
		}
		
		/**
		 * 频道按键点击事件
		 */
		protected function itemClick(e:MouseEvent):void
		{	
			if(!this.getStyle("enabled"))
				return;
			
			var b:Button=e.currentTarget as Button;
			var i:int=getIndexByName(b.name);
			var lay:String=getStyleDefault("layout","horizontal");//默认水平排列
		
			if(lay.toLocaleLowerCase()=="accordion") //替换accordion控件
			{
				if(b.selected==true)
					this.selectedIndex=-1;
				else
					selectedIndex=i;
			}else
				selectedIndex=i;

			try{
				if(_clickFunction!=null)
					this._clickFunction(e);	
			}catch(e:Error)
			{
				trace(e);
			}
		}
		
		/**
		 * 选择改变
		 */
		protected function _onTbChange(listBase:ListBase):void
		{
			if(this.selectedItem!=null)
				(this.selectedItem as Button).selected=true;
			if(lastSelectedItem is Button)
				lastSelectedItem.selected=false;
			
			if(_list && _list.length>this.selectedIndex && selectedIndex>=0)
				_list[selectedIndex].visible=true;
			
			if(_list && _list.length>this.lastSelected && lastSelected>=0)
				_list[lastSelected].visible=false;
			
			merge();
			
		}
		
		
		//private	
		private var _list:Array;//对应的panel列表
		private var _buttons:Array;//按钮
		private var _clickFunction:Function;//选择tab按钮调用的function(this)
		private var _preItemBind:Function;//绑定之前 一般用于修改特定样式。
		private var _buttonClassName:String;
		private var _colorFilter:ColorMatrixFilter;

		/**
		 * tab按钮的样式名
		 */
		public function get buttonClassName():String
		{
			return _buttonClassName;
		}

		/**
		 * @private
		 */
		public function set buttonClassName(value:String):void
		{
			_buttonClassName = value;
		}

		/**
		 * 对应的panel列表
		 */
		public function get list():Array
		{
			if(!_list)
			{
				this.createPanel();
				if(this.initialized)
				{
					addPanel();
					this.merge();
				}
			}
			return _list;
		}

		/**
		 * @private
		 */
		public function set list(value:Array):void
		{
			if(_list)
			{
				this.removeAllChild();
				_list.length=0;
			}
			_list = value;
			if(this.initialized)
			{
				addPanel();
				this.merge();
			}
		}

		/**
		 * 按钮
		 */
		public function get buttons():Array
		{
			return _buttons;
		}
		
		//

	}
}